package com.ops.service.ns.system.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import com.ops.schema.ns.system.OPSLookUps;
import com.ops.schema.ns.system.OPSRecordAuditInfo;
import com.ops.schema.ns.system.OPSRuleBinds;
import com.ops.schema.ns.system.SearchLookUpsRequest;
import com.ops.schema.ns.system.SearchLookUpsResponse;
import com.ops.schema.ns.system.SearchRuleBindsRequest;
import com.ops.schema.ns.system.SearchRuleBindsResponse;
import com.ops.service.ns.system.client.OnlinePayrollSystemServiceClient;
import com.ops.service.ns.system.exception.ServiceException;

public class RuleBindMBean {

	private String ruleName;
	private String ruleVar;
	private String bindType;
	private String bindLevel;
	private List<SelectItem> bindTypeList;
	private List<OPSRuleBinds> opsruleBindlst;
	
	public void searchRuleBinds() {
		System.out.println("Inside searchRules");
		SearchRuleBindsRequest rulesRequest = new SearchRuleBindsRequest();
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		uditInfo.setAction("S");
		uditInfo.setObjectType(getMenuId());
		rulesRequest.setOPSRecordAuditInfo(uditInfo);
		try {
			SearchRuleBindsResponse ruleBindsResponse = OnlinePayrollSystemServiceClient
					.searchRuleBind(rulesRequest);
			setOpsruleBindlst(ruleBindsResponse.getOPSRuleBinds());

		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void resetRuleBinds() {
		setRuleName(null);
		setRuleVar(null);
		setBindLevel(null);
		setBindType(null);
	}
	
	
	private String getSessionId() {

		FacesContext fCtx = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fCtx.getExternalContext()
				.getSession(false);
		return session.getId();
	}

	private String getuserId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("UserId").toString();
	}
	private String getMenuId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("MenuId").toString();
	}
	
	
	public String getRuleName() {
		return ruleName;
	}

	public void setRuleName(String ruleName) {
		this.ruleName = ruleName;
	}

	public String getRuleVar() {
		return ruleVar;
	}
	public void setRuleVar(String ruleVar) {
		this.ruleVar = ruleVar;
	}
	public String getBindType() {
		return bindType;
	}
	public void setBindType(String bindType) {
		this.bindType = bindType;
	}
	public String getBindLevel() {
		return bindLevel;
	}
	public void setBindLevel(String bindLevel) {
		this.bindLevel = bindLevel;
	}

	

	public List<OPSRuleBinds> getOpsruleBindlst() {
		return opsruleBindlst;
	}

	public void setOpsruleBindlst(List<OPSRuleBinds> opsruleBindlst) {
		this.opsruleBindlst = opsruleBindlst;
	}

	public List<SelectItem> getBindTypeList() {
		if (bindTypeList == null) {
			bindTypeList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			bindTypeList.add(defitem);
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			masterRequest.setType("RULEBIND-TYPE");
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps master : masterResponse.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					bindTypeList.add(item);
				}
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return bindTypeList;
	}

	public void setBindTypeList(List<SelectItem> bindTypeList) {
		this.bindTypeList = bindTypeList;
	}
	
}
